package cn.jbolt.common.base;

import java.util.Map;
import com.alibaba.fastjson.JSONObject;

/**
 * Created by 160494 on 2017/7/14.
 */
public class BaseRepMsg extends JSONObject {
    private static final long serialVersionUID = 5813258945341182584L;
    private static final Integer DEFAULT_CODE= MsgCodeEnum.REQUEST_SUCCESS_0.getCode();
    private static final String DEFAULT_MSG="success";
    private static final String DEFAULT_MSG_CN="请求成功";
    private static final String DEFAULT_VERSION="1.0.0";



    public BaseRepMsg() {
        super();
        initBaseMag();
    }

    public BaseRepMsg(Map<String, Object> map) {
        super(map);
        initBaseMag();
    }

    public Integer getCode() {
        return this.getInteger("code");
    }

    public void setCode(Integer code) {
        this.set("code",code);
    }

    public String getMsg() {
        return this.getString("msg");
    }

    public void setMsg(String msg) {
        this.set("msg",msg);
    }

    public String getMsgCn() {
        return this.getString("msg_cn");
    }

    public void setMsgCn(String msgCn) {
        this.set("msg_cn",msgCn);
    }

    public String getVersion() {
        return this.getString("version");
    }

    public void setVersion(String version) {
        this.set("version",version);
    }

    public Long getTimestamp() {
        return this.getLong("timestamp");
    }

    public void setTimestamp(Long timestamp) {
        this.set("timestamp",timestamp);
    }

    private void initBaseMag(){
      setCode(DEFAULT_CODE);
      setMsg(DEFAULT_MSG);
      setMsgCn(DEFAULT_MSG_CN);
      setVersion(DEFAULT_VERSION);
      setTimestamp(System.currentTimeMillis());//当前时间的时间戳
    }

    public BaseRepMsg getEntity(String key) {
        Object value = this.get(key);
        if (value instanceof BaseRepMsg) {
            return (BaseRepMsg) value;
        }

        JSONObject jobj = null;

        if (value instanceof JSONObject) {
            jobj = (JSONObject) value;
        } else {
            jobj = (JSONObject) toJSON(value);
        }

        return jobj == null ? null : new BaseRepMsg(jobj);
    }

    public BaseRepMsg set(String key, Object value, boolean force) {
        if (force || value != null) {
            super.put(key, value);
        }
        return this;
    }

    public BaseRepMsg set(String key, Object value) {
        return this.set(key, value, true);
    }

    public BaseRepMsg setAll(Map<? extends String, ? extends Object> m) {
        super.putAll(m);
        return this;
    }

    //参数错误
    public BaseRepMsg paramError(){
        this.setCode(MsgCodeEnum.PARAM_ERROR_12.getCode());
        this.setMsg("error");
        this.setMsgCn(MsgCodeEnum.PARAM_ERROR_12.getMsg());
        this.setVersion(DEFAULT_VERSION);
        this.setTimestamp(System.currentTimeMillis());
        return this;
    }

    //返回成功
    public BaseRepMsg success(){
    	this.initBaseMag();
        return this;
    }
    //返回失败
    public BaseRepMsg badRequest(){
        this.setCode(MsgCodeEnum.REQUEST_FAIL_2.getCode());
        this.setMsg("error");
        this.setMsgCn("请求失败");
        this.setVersion(DEFAULT_VERSION);
        this.setTimestamp(System.currentTimeMillis());
        return this;
    }
/*
    //指令下发成功
    public BaseRepMsg cmdSuccess(){
        this.setCode(MsgCodeEnum.REQUEST_SUCCESS_0.getCode());
        this.setMsg("success");
        this.setMsgCn("指令下发成功");
        this.setVersion(DEFAULT_VERSION);
        this.setTimestamp(System.currentTimeMillis());
        return this;
    }

    //指令下发失败
    public BaseRepMsg cmdError(){
        this.setCode(MsgCodeEnum.REQUEST_FAIL_2.getCode());
        this.setMsg("error");
        this.setMsgCn("指令下发失败");
        this.setVersion(DEFAULT_VERSION);
        this.setTimestamp(System.currentTimeMillis());
        return this;
    }
*/
    //设备不存在
    public BaseRepMsg deviceNotExist(){
        this.setCode(MsgCodeEnum.NOT_FOUND_13.getCode());
        this.setMsg("error");
        this.setMsgCn(MsgCodeEnum.NOT_FOUND_13.getMsg());
        this.setVersion(DEFAULT_VERSION);
        this.setTimestamp(System.currentTimeMillis());
        return this;
    }

    //设备已被其他设备绑定
    public BaseRepMsg deviceIsBind(){
        this.setCode(MsgCodeEnum.DEVICE_IS_BIND_34002.getCode());
        this.setMsg("error");
        this.setMsgCn(MsgCodeEnum.DEVICE_IS_BIND_34002.getMsg());
        this.setVersion(DEFAULT_VERSION);
        this.setTimestamp(System.currentTimeMillis());
        return this;
    }

    //设备当前状态不可绑定
    public BaseRepMsg deviceNotBind(){
        this.setCode(MsgCodeEnum.DEVICE_NOT_BIND_34001.getCode());
        this.setMsg("error");
        this.setMsgCn(MsgCodeEnum.DEVICE_NOT_BIND_34001.getMsg());
        this.setVersion(DEFAULT_VERSION);
        this.setTimestamp(System.currentTimeMillis());
        return this;
    }
    /**
     * 请求是否成功
     * @return
     */
    public boolean isSucess() {
    	return this.getCode() == MsgCodeEnum.REQUEST_SUCCESS_0.getCode();
    }
}
